package com.fyl.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fyl.pojo.User;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.http.HttpRequest;
import java.util.List;
import java.util.Map;

@Controller
public class ParamController {

    //http://localhost:8080/param1?username=zhansan&age=14
    @GetMapping("/param1")
    public String param1(String username, int age) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        return "/index.jsp";
    }

    //http://localhost:8080/param2?username=zhansan&age=14
    @GetMapping("/param2")
    public String param2(@RequestParam(name = "username", required = true, defaultValue = "123") String username1, @RequestParam("age") int age1) {
        System.out.println("username1 = " + username1);
        System.out.println("age1 = " + age1);
        return "/index.jsp";
    }

    //http://localhost:8080/param3?hobby=zhansan&hobby=14
    @GetMapping("/param3")
    public String param3(String[] hobby) {
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/index.jsp";
    }

    //http://localhost:8080/param4?hobby=zhansan&hobby=14
    @GetMapping("/param4")
    public String param4(@RequestParam List<String> hobby) {
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/index.jsp";
    }

    //http://localhost:8080/param5?username=zhansan&age=14
    @GetMapping("/param5")
    public String param5(@RequestParam Map<String, String> map) {
        map.forEach((k, v) -> System.out.println(k + " = " + v));
        return "/index.jsp";
    }

    //http://localhost:8080/param6?username=zhansan&age=14&hobbies=zhansan&hobbies=14&birthday=2025/11/11&address.city=jx&address.area=nh
    @GetMapping("/param6")
    public String param6(User user) {
        System.out.println("user = " + user);
        return "/index.jsp";
    }

    //http://localhost:8080/param7
    @PostMapping("/param7")
    public String param7(@RequestBody String body) {
        System.out.println("body = " + body);
        return "/index.jsp";
    }

    //http://localhost:8080/param8
    @PostMapping("/param8")
    public String param8(@RequestBody String body) throws JsonProcessingException {
        System.out.println("body = " + body);
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(body, User.class);
        System.out.println("user = " + user);
        return "/index.jsp";
    }

    //http://localhost:8080/param9
    @PostMapping("/param9")
    public String param9(@RequestBody User user) {
        System.out.println("user = " + user);
        return "/index.jsp";
    }

    //http://localhost:8080/user/100/zhangsan
    @GetMapping("/user/{id}/{username}")
    public String param10(@PathVariable("id") Integer id,
                          @PathVariable("username") String username) {
        System.out.println("id = " + id);
        System.out.println("username = " + username);
        return "/index.jsp";
    }

    //http://localhost:8080/param11
    @PostMapping("/param11")
    public String param11(@RequestBody MultipartFile file) throws IOException {
        System.out.println("file = " + file);
        //获取输入流
        InputStream inputStream = file.getInputStream();
        //定义输出流
        OutputStream outputStream = new FileOutputStream("C:\\Users\\13037\\Desktop\\新建文件夹\\"+file.getOriginalFilename());
        //拷贝
        IOUtils.copy(inputStream, outputStream);
        outputStream.close();
        inputStream.close();
        return "/index.jsp";
    }

    //http://localhost:8080/param12
    @GetMapping("/param12")
    public String param12(@RequestHeader("accept-encoding") String headerValue) throws IOException {
        System.out.println("headerValue = " + headerValue);
        return "/index.jsp";
    }

    //http://localhost:8080/param13
    @GetMapping("/param13")
    public String param13(@RequestHeader Map<String,String> map) throws IOException {
       map.forEach((k,v) -> System.out.println(k + " = " + v));
        return "/index.jsp";
    }

    //http://localhost:8080/param14
    @GetMapping("/param14")
    public String param14(@CookieValue("JSESSIONID") String jsessionid) throws IOException {
        System.out.println("jsessionid = " + jsessionid);
        return "/index.jsp";
    }

    @GetMapping("/request1")
    public String request1(HttpServletRequest request){
        //向request域中存储数据
        request.setAttribute("name","lele");
        return "/request2";
    }

    @GetMapping("/request2")
    public String request2(@RequestAttribute("name") String name){
        //向request域中存储数据
        System.out.println("name = " + name);
        return "/index.jsp";
    }

    @GetMapping("/param15")
    public String param15(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("request = " + request);
        System.out.println("response = " + response);
        return "/index.jsp";
    }
}
